package tgc.edu.tms.web.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import tgc.edu.tms.custom.AjaxResult;
import tgc.edu.tms.custom.CommonController;
import tgc.edu.tms.entity.SysRole;
import tgc.edu.tms.entity.Teacher;
import tgc.edu.tms.service.SysRoleService;
import tgc.edu.tms.service.TeacherService;
import tgc.edu.tms.web.form.TeacherForm;

@Controller
@RequestMapping(value="/teacher")
public class TeacherController extends CommonController<Teacher, Integer, TeacherForm>{
	@Autowired
	private TeacherService service;
	@Autowired
	private SysRoleService sysRoleService;

	@Override
	public void edit(TeacherForm form, ModelMap map) throws InstantiationException, IllegalAccessException {
		List<SysRole> roles=sysRoleService.findByParentIsNull();
		map.put("roles", roles);
		super.edit(form, map);
	}

	@Override
	public Object save(TeacherForm form) {
		BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
		try {
			Teacher model=new Teacher(); 
			Integer id = form.getId();
			if(id!=null) {
				model=service.findById(id);
			}
			String password = form.getPassword();
			if(StringUtils.hasText(password)) {
				password=encoder.encode(password);
				model.setPassword(password);
			}
			if(!StringUtils.hasText(model.getPassword())) {
				model.setPassword(encoder.encode("123456"));
			}
			BeanUtils.copyProperties(form, model,"id","password");
			
			List<SysRole> roles = new ArrayList<>();
			for (String code : form.getRoles().split(",")) {
				roles.add(sysRoleService.findbyCode(code));
			}
			model.setRoles(roles);
			service.save(model);
			return new AjaxResult("OK");
		}catch (Exception e) {
			return new AjaxResult(false, "数据保存失败");
		}
	}

	@Override
	public Specification<Teacher> buildSpec(TeacherForm form) {
		HashSet<Rule> rules=new HashSet<>();
		String name = form.getName();
		if(StringUtils.hasText(name)) {
			rules.add(Rule.property("name").contain(name));
		}
		String username = form.getUsername();
		if(StringUtils.hasText(username)) {
			rules.add(Rule.property("username").contain(username));
		}
		String search = form.getSearch();
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("name").contain(search));
			rules2.add(Rule.property("username").contain(search));
			rules2.add(Rule.property("phone").contain(search));
			rules2.add(Rule.property("email").contain(search));
			rules2.add(Rule.property("addrs").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
}
